package com.agileai.bpm.controller.define;

import java.util.ArrayList;
import java.util.List;

import com.agileai.bpm.model.ActivitiesDocument.Activities;
import com.agileai.bpm.model.ForkActivityType;
import com.agileai.bpm.model.ProcessDefineDocument.ProcessDefine;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;

public class WfForkActivitySelectHandler extends WfProcessInfoHandler{
	public WfForkActivitySelectHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param){
		try {
			ProcessDefine processDefine = getProcessDefine();
			Activities activities =  processDefine.getActivities();
			List<DataRow> processVars = null;
			if (activities != null){
				processVars = buildProcessVars(activities);
			}
			this.setRsList(processVars);
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new LocalRenderer(getPage());
	}
	
	private List<DataRow> buildProcessVars(Activities activities){
		List<DataRow> result = new ArrayList<DataRow>();
		ForkActivityType[] forkActivityTypes = activities.getForkActivityArray();
		if (forkActivityTypes != null){
			for (int i=0; i< forkActivityTypes.length;i++){
				ForkActivityType forkActivityType = forkActivityTypes[i];
				DataRow row = new DataRow();
				row.put("ACTIVITY_NAME",forkActivityType.getName());
				row.put("ACTIVITY_CODE",forkActivityType.getCode());
				row.put("ACTIVITY_ID",forkActivityType.getId());
				result.add(row);
			}
		}
		return result;
	}
}
